//
//  KDFocusListCellModelFrame.m
//  YuQingXiaoFang
//
//  Created by wangdongdong on 18/6/15.
//  Copyright © 2018年 KDTech. All rights reserved.
//

#import "KDFocusListCellModelFrame.h"
#import "KDFocusListCellModel.h"
/** cell的边框宽度 */
#define KDNewsCellBorder 5
@implementation KDFocusListCellModelFrame
-(void) setModel:(KDFocusListCellModel *)model{
    _model=model;
    
    //标题
    CGFloat titleLabelX=KDNewsCellBorder;
    CGFloat titleLabelY=KDNewsCellBorder;
    CGSize titleLabelSize=[model.newstitle sizeWithAttributes:@{NSFontAttributeName:KDNewsCellModelTitleFont} ];
    CGFloat titleW=titleLabelSize.width;
    //标题超出屏幕宽度，则设置为屏幕宽度
    if(titleW>(screenW-KDNewsCellBorder-titleLabelX)){
        titleLabelSize.width=screenW-KDNewsCellBorder-titleLabelX;
    }
    _titleLabelF=(CGRect){{titleLabelX,titleLabelY},titleLabelSize};
    
    //时间
    CGFloat timeLabelX=titleLabelX;
    CGFloat timeLabelY=CGRectGetMaxY(_titleLabelF)+KDNewsCellBorder;
    NSString *pushTimes=[NSString stringWithFormat:@"%ld",model.attentTime];
    CGSize timeLabelSize=[pushTimes sizeWithAttributes:@{NSFontAttributeName :KDNewsCellModelTimeFont}];
    timeLabelSize.width=110;
    _timeLabelF=(CGRect){{timeLabelX,timeLabelY},timeLabelSize};
    
    //来源或作者
    CGFloat sourceLabelX=CGRectGetMaxX(_timeLabelF)+KDNewsCellBorder;
    CGFloat sourceLabelY=timeLabelY;
    CGSize sourceLabelSize=[model.newssource sizeWithAttributes:@{NSFontAttributeName :KDNewsCellModelTimeFont}];
    _sourceLabelF=(CGRect){{sourceLabelX,sourceLabelY},sourceLabelSize};
    
    //内容
    CGFloat contentLabelX=titleLabelX;
    CGFloat contentLabelY=CGRectGetMaxY(_timeLabelF)+KDNewsCellBorder;
    CGFloat contentLabelMaxW=screenW-KDNewsCellBorder*2;
    //计算单行高度，为了处理bug（比如5行显示2行字，另外3行空白）
    CGFloat contentLabelH=[self getContentLabelSize:@"单行高度" AndContentLabelMaxW:contentLabelMaxW].height;
    //计算内容高度
    CGSize contentLabelSize = [self getContentLabelSize:model.newssummary AndContentLabelMaxW:contentLabelMaxW];
    //大于1行时，显示2行；否则显示1行
    //if(contentLabelSize.height>contentLabelH){
        contentLabelSize.height=contentLabelH*2;
   // }else{
    //    contentLabelSize.height=contentLabelH;
    //}
    _contentLabelF=(CGRect){{contentLabelX,contentLabelY},contentLabelSize};
    
    //取消关注
    CGSize cancelButtonSize=[@"取消" sizeWithAttributes:@{NSFontAttributeName:KDNewsCellModelButtonFont}];
    CGFloat cancelButtonX=screenW-KDNewsCellBorder-cancelButtonSize.width;
    CGFloat cancelButtonY=CGRectGetMaxY(_contentLabelF)+KDNewsCellBorder;
    _cancelButtonF=(CGRect){{cancelButtonX,cancelButtonY},cancelButtonSize};
    
    //预警级别
    CGFloat starViewW=_cancelButtonF.size.height;
    CGFloat starViewH=_cancelButtonF.size.height;
    CGFloat starViewY=cancelButtonY;
    //低级
    if(model.newsLevel==0 || model.newsLevel==1 || model.newsLevel==2 || model.newsLevel==3){
        CGFloat star1ViewX;
        //是否领导批示 1为已批
        star1ViewX=cancelButtonX-KDNewsCellBorder-starViewW;
        _star1ViewF=CGRectMake(star1ViewX, starViewY, starViewW, starViewH);
    }
    //中级
    if(model.newsLevel==2 || model.newsLevel==3){
        CGFloat star2ViewX=_star1ViewF.origin.x-starViewW;
        _star2ViewF=CGRectMake(star2ViewX, starViewY, starViewW, starViewH);
    }
    //高级
    if(model.newsLevel==3){
        CGFloat star3ViewX=_star2ViewF.origin.x-starViewW;
        _star3ViewF=CGRectMake(star3ViewX, starViewY, starViewW, starViewH);
    }

    
    // 计算cell的高度
    _cellHeight =  CGRectGetMaxY(_cancelButtonF)+KDNewsCellBorder;
    
}
//计算内容size
-(CGSize) getContentLabelSize:(NSString*)contentStr AndContentLabelMaxW:(CGFloat)contentLabelMaxW{
    CGSize contentLabelSize= [contentStr boundingRectWithSize:CGSizeMake(contentLabelMaxW, MAXFLOAT)
                                                      options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{NSFontAttributeName :KDNewsCellModelContentFont} context:nil].size;
    return contentLabelSize;
}
@end
